<script>
import { versionUpdate } from '@/services/user';

export default {
  onLaunch() {
    //检查版本升级及下载安装apk自动升级
    // #ifdef APP-PLUS
    this.checkUpdate(plus.runtime.version);

    //缓存共用本地存储变量
    uni.setStorageSync(
      'Version' + process.env.VUE_APP_PLATFORMID,
      plus.runtime.version
    );
    // #endif
  },
  methods: {
    //版本号检查&检查更新
    async checkUpdate(version) {
      const { code, data } = await versionUpdate(version);
      if (code != 200) return;
      if (data.update) {
        this._download(data.url);
      }
    },
    //下载方法
    _download(url) {
      const dtask = plus.downloader.createDownload(url, {}, (d, status) => {
        //console.log(d, status);
        uni.showToast({
          title: '下载完成',
          icon: 'none',
        });
        if (status == 200) {
          plus.runtime.install(
            plus.io.convertLocalFileSystemURL(d.filename),
            {},
            () => {
              uni.showToast({
                title: '下载成功',
                icon: 'none',
              });
            },
            () => {
              uni.showToast({
                title: '安装失败-01',
                icon: 'none',
              });
            }
          );
        } else {
          uni.showToast({
            title: '更新失败-02',
            icon: 'none',
          });
        }
      });
      try {
        const loading = plus.nativeUI.showWaiting('正在下载', {
          back: 'none',
        });
        dtask.start();
        dtask.addEventListener('statechanged', (task, status) => {
          switch (task.state) {
            case 3:
              {
                const { downloadedSize, totalSize } = task;
                const progress = parseInt((downloadedSize / totalSize) * 100);
                loading.setTitle(`正在下载${progress}%`);
              }
              break;
            case 4:
              plus.nativeUI.closeWaiting();
              break;
          }
        });
      } catch {
        plus.nativeUI.closeWaiting();
        uni.showToast({
          title: '更新失败-03',
          icon: 'none',
        });
      }
    },
  },
};
</script>

<style lang="scss">
@import './static/fonts/iconfont.css';
@import 'tailwindcss/utilities';
@import 'uview-ui/index.scss';

*,
::before,
::after {
  box-sizing: border-box;
  border-width: 0;
  border-style: solid;
  border-color: currentColor;
}

//2023.08.07
.bg-inputGray {
  background: #f2f2f2;
}
.bg-inputGray-reg {
  background: none;
  border: 1px solid #333333;
}
.bg-inputOrange {
  background: #d62849;
}
.border-common {
  border: 2rpx solid #333;
}
.border-common-gray {
  border: 2rpx solid #ccc;
}
.border-tb-gray {
  border-top: 2rpx solid #ccc;
  border-bottom: 2rpx solid #ccc;
}
.border-b-orange {
  border-bottom: 2rpx solid #ffa829;
}
.border-t-gray {
  border-top: 2rpx solid #ccc;
}
.border-b-gray {
  border-bottom: 2rpx solid #ccc;
}
.border-b-grayLights {
  border-bottom: 2rpx solid #eeeeee;
}
.moreBoxLine {
  border-bottom: 2rpx solid #888;
}
.border-b-grayLight {
  border-bottom: 2rpx solid #ddd;
}
.border-b-gray-dashed {
  border-bottom: 2rpx dashed #ccc;
}
.border-l-gray-dashed {
  border-left: 2rpx dashed #ccc;
}
.login-btn {
  background: #ff3d62;
  box-shadow: 0px 12px 26px 0px #cccccc;
}
.common-box-shadow {
  box-shadow: 0px 12px 26px 0px #cccccc;
}
.bg-box-shadow {
  box-shadow: 0px 12px 26px 0px #e5e5e5;
}
.indexLine {
  position: absolute;
  right: 0;
  top: 50%;
  margin-top: -20rpx;
  width: 2rpx;
  height: 40rpx;
  background: #999999;
}
.common-btn {
  width: 100%;
  height: 84rpx;
  line-height: 84rpx;
  box-shadow: 0px 12px 26px 0px #cccccc;
}
.item-3-10 {
  width: 30%;
}
.item-mx-5 {
  margin-left: 5%;
  margin-right: 5%;
}
.activeIndex {
  color: #ff3d62;
  border: 1px solid #ff3d62;
}
.diy-triangle-up {
  width: 0;
  height: 0;
  border: 8rpx solid transparent;
  border-bottom-color: rgba(0, 0, 0, 0.6);
}
.moreBox {
  background: rgba(0, 0, 0, 0.6);
}
.button-bg {
  background: rgba(0, 0, 0, 0.4);
}
.common-bar {
  width: 142rpx;
  height: 8rpx;
  background: #e5e5e5;
  border-radius: 4rpx;
}
.customer-bottom-bg {
  background: #f6f6f6;
}
.transitionLinear {
  transition: all 0.3s linear;
}
.default-mr-3 {
  margin-right: 3%;
}
.orderTypeCur {
  color: #fff;
  background: #ff4b83;
  border: 1px solid #fff;
}
.order-btn-border {
  border: 2rpx solid #ff3d62;
}
.order-btn-border-black {
  border: 2rpx solid #333;
}
.bg-orderList {
  background: linear-gradient(180deg, #ff568c, #fe74a0);
}
.businessBox {
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0px 14px 81px 0px rgba(214, 197, 203, 0.3);
  width: 100%;
  height: auto;
}
</style>
